﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SharpWeb
{
    /// <summary>
    /// map标签，定义一个客户端图像映射。图像映射（image-map）指带有可点击区域的一幅图像。 
    /// </summary>
    public class MapTag : AbstractContainerTagComponent
    {

        public MapTag()
        {
            _Areas = new AreaTagCollection();
        }
        protected override bool CloseItSelf
        {
            get
            {
                return false;
            }
        }

        protected override string TagName
        {
            get { return "map"; }
        }

        private readonly AreaTagCollection _Areas;

        public AreaTagCollection Areas
        {
            get { return _Areas; }
        }

        protected override void OnRenderContent(StringBuilder sb, ComponentContext context)
        {
            base.OnRenderContent(sb, context);
            foreach (var item in Areas)
            {
                item.RenderComponent(sb, context);
            }
        }

    }
    /// <summary>
    /// 定义图像映射中的区域（注：图像映射指得是带有可点击区域的图像）。
    /// </summary>
    public class AreaTag : TagComponent
    {
        protected override string TagName
        {
            get { return "area"; }
        }
        protected override bool CloseItSelf
        {
            get
            {
                return true;
            }
        }
        /// <summary>
        /// 定义区域的形状。
        /// <list type="string">
        /// <item>default</item>
        /// <item>rect</item>
        /// <item>circ</item>
        /// <item>poly</item>
        /// </list>
        /// </summary>
        public string shape
        {
            get { return this.Attributes["shape"]; }
            set { this.Attributes["shape"] = value; }
        }
        /// <summary>
        /// 定义可点击区域（对鼠标敏感的区域）的坐标。
        /// </summary>
        public string coords
        {
            get { return this.Attributes["coords"]; }
            set { this.Attributes["coords"] = value; }
        }
        /// <summary>
        /// 定义此区域的目标 URL。
        /// </summary>
        public string href
        {
            get { return this.Attributes["href"]; }
            set { this.Attributes["href"] = value; }
        }
        /// <summary>
        /// 规定在何处打开 href 属性指定的目标 URL。
        /// </summary>
        public string target
        {
            get { return this.Attributes["href"]; }
            set { this.Attributes["href"] = value; }
        }

        /// <summary>
        /// 定义此区域的替换文本。
        /// </summary>
        public string alt
        {
            get { return this.Attributes["alt"]; }
            set { this.Attributes["alt"] = value; }
        }

    }
    /// <summary>
    /// 定义Area的图形
    /// </summary>
    public enum AreaShape
    {
        @default,
        rect,
        circ,
        poly,

    }
    /// <summary>
    /// Area元素的集合
    /// </summary>
    public class AreaTagCollection : KeyedCollection<string, AreaTag>
    {
        public AreaTag Add(AreaShape shape, string coords, string href, string alt)
        {
            AreaTag tag = new AreaTag()
            {
                shape = System.Enum.GetName(typeof(AreaShape), shape),
                coords = coords,
                href = href,
                alt = alt
            };
            this.Add(tag);
            return tag;
        }
        protected override string GetKeyForItem(AreaTag item)
        {
            return item.ComponentName;
        }
    }
}
